package com.fone.player.storage;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

import com.fone.player.entity.ContentSubscribe;
import com.fone.player.util.L;

/**
 * 
 * 节目预约表适配器
 * 
 * @author wangyang
 * @since 2014年4月29日
 */
class ContentSubscribeDataBaseAdapter implements IFoneDatabase {
	private static final String TAG = ContentSubscribeDataBaseAdapter.class
			.getSimpleName();
	private static final ContentSubscribeDataBaseAdapter mInstance = new ContentSubscribeDataBaseAdapter();
	private OperateDataBaseTemplate mGeneralDataBaseTemplate = OperateDataBaseTemplate
			.getInstance();

	private ContentSubscribeDataBaseAdapter() {

	}

	static ContentSubscribeDataBaseAdapter getInstance() {
		return mInstance;
	}

	/**
	 * 
	 * 添加预约节目
	 * 
	 * @param contentSubscribe
	 *            预约节目
	 * @return int 最新插入的id<br>
	 *         -1 插入异常
	 * 
	 */
	public int addContentSubscribe(ContentSubscribe contentSubscribe) {
		synchronized (TAG) {
			int id = -1;
			try {
				mGeneralDataBaseTemplate.open();
				ContentValues contentValues = new ContentValues();
				contentValues.put(CONTENT_SUBSCRIBE_NAME,
						contentSubscribe.getContentSubscribeName());
				contentValues.put(CONTENT_SUBSCRIBE_URL,
						contentSubscribe.getContentSubscribeUrl());
				contentValues.put(CONTENT_SUBSCRIBE_TIME,
						contentSubscribe.getContentSubscribeTime());
				long newId = mGeneralDataBaseTemplate.insert(
						TB_CONTENT_SUBSCRIBE, null, contentValues);
				id = (int) newId;

			} catch (Exception e) {
				e.printStackTrace();
				L.v(TAG, e.getMessage());
				return -1;
			} finally {

				mGeneralDataBaseTemplate.close();
			}
			return id;
		}
	}

	/**
	 * 
	 * 根据ID获得预约节目
	 * 
	 * @param contentSubscribeId
	 *            预约节目id
	 * @return ContentSubscribe 预约节目
	 */
	public ContentSubscribe getContentSubscribeById(int contentSubscribeId) {
		StringBuilder sql = new StringBuilder("select * from ");
		sql.append(TB_CONTENT_SUBSCRIBE);
		sql.append(" where ");
		sql.append(CONTENT_SUBSCRIBE_ID);
		sql.append("=");
		sql.append(contentSubscribeId);
		ContentSubscribe contentSubscribe = null;
		Cursor cursor = null;
		try {
			mGeneralDataBaseTemplate.open();
			cursor = mGeneralDataBaseTemplate.select(sql.toString());

			if (cursor == null) {
				return null;
			}

			// 读取数据
			while (cursor.moveToNext()) {
				contentSubscribe = new ContentSubscribe();
				contentSubscribe.setContentSubscribeId(cursor.getInt(cursor
						.getColumnIndex(CONTENT_SUBSCRIBE_ID)));
				contentSubscribe.setContentSubscribeName(cursor
						.getString(cursor
								.getColumnIndex(CONTENT_SUBSCRIBE_NAME)));
				contentSubscribe.setContentSubscribeUrl(cursor.getString(cursor
						.getColumnIndex(CONTENT_SUBSCRIBE_URL)));
				contentSubscribe.setContentSubscribeTime(cursor.getLong(cursor
						.getColumnIndex(CONTENT_SUBSCRIBE_TIME)));
			}

		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (cursor != null) {

				cursor.close();

			}
			mGeneralDataBaseTemplate.close();

		}
		return contentSubscribe;
	}
	
	/**
	 * 
	 * 获取距离当前时间最近的节目单
	 * 
	 * @return ContentSubscribe 预约节目
	 */
	public ContentSubscribe getNearestContentSubscribe() {
		StringBuilder sql = new StringBuilder("select * from ");
		sql.append(TB_CONTENT_SUBSCRIBE);
		sql.append(" order by ");
		sql.append(CONTENT_SUBSCRIBE_TIME);
		sql.append(" asc limit 1 offset 0");
		ContentSubscribe contentSubscribe = null;
		Cursor cursor = null;
		try {
			mGeneralDataBaseTemplate.open();
			cursor = mGeneralDataBaseTemplate.select(sql.toString());

			if (cursor == null) {
				return null;
			}

			// 读取数据
			while (cursor.moveToNext()) {
				contentSubscribe = new ContentSubscribe();
				contentSubscribe.setContentSubscribeId(cursor.getInt(cursor
						.getColumnIndex(CONTENT_SUBSCRIBE_ID)));
				contentSubscribe.setContentSubscribeName(cursor
						.getString(cursor
								.getColumnIndex(CONTENT_SUBSCRIBE_NAME)));
				contentSubscribe.setContentSubscribeUrl(cursor.getString(cursor
						.getColumnIndex(CONTENT_SUBSCRIBE_URL)));
				contentSubscribe.setContentSubscribeTime(cursor.getLong(cursor
						.getColumnIndex(CONTENT_SUBSCRIBE_TIME)));
			}

		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (cursor != null) {

				cursor.close();

			}
			mGeneralDataBaseTemplate.close();

		}
		return contentSubscribe;
	}

	/**
	 * 
	 * 根据名字和时间获得预约节目
	 * 
	 * @param contentSubscribeName
	 *            预约节目名字
	 * @return ContentSubscribe 预约节目
	 */
	public ContentSubscribe getContentSubscribeByNameAndTime(
			String contentSubscribeName, long contentSubscribeTime) {
		StringBuilder sql = new StringBuilder("select * from ");
		sql.append(TB_CONTENT_SUBSCRIBE);
		sql.append(" where ");
		sql.append(CONTENT_SUBSCRIBE_NAME);
		sql.append("='");
		sql.append(contentSubscribeName);
		sql.append("'");
		sql.append(" and ");
		sql.append(CONTENT_SUBSCRIBE_TIME);
		sql.append("=");
		sql.append(contentSubscribeTime);
		ContentSubscribe contentSubscribe = null;
		Cursor cursor = null;
		try {
			mGeneralDataBaseTemplate.open();
			cursor = mGeneralDataBaseTemplate.select(sql.toString());

			if (cursor == null) {
				return null;
			}

			// 读取数据
			while (cursor.moveToNext()) {
				contentSubscribe = new ContentSubscribe();
				contentSubscribe.setContentSubscribeId(cursor.getInt(cursor
						.getColumnIndex(CONTENT_SUBSCRIBE_ID)));
				contentSubscribe.setContentSubscribeName(cursor
						.getString(cursor
								.getColumnIndex(CONTENT_SUBSCRIBE_NAME)));
				contentSubscribe.setContentSubscribeUrl(cursor.getString(cursor
						.getColumnIndex(CONTENT_SUBSCRIBE_URL)));
				contentSubscribe.setContentSubscribeTime(cursor.getLong(cursor
						.getColumnIndex(CONTENT_SUBSCRIBE_TIME)));
			}

		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (cursor != null) {

				cursor.close();

			}
			mGeneralDataBaseTemplate.close();

		}
		return contentSubscribe;
	}

	/**
	 * 
	 * 获得所有预约节目列表
	 * 
	 * @return List<ContentSubscribe> 预约节目集合
	 */
	public List<ContentSubscribe> getContentSubscribeList() {
		StringBuilder sql = new StringBuilder("select * from ");
		sql.append(TB_CONTENT_SUBSCRIBE);

		Cursor cursor = null;
		List<ContentSubscribe> contentSubscribeList = null;
		try {
			mGeneralDataBaseTemplate.open();
			cursor = mGeneralDataBaseTemplate.select(sql.toString());

			if (cursor == null) {
				return null;
			}

			contentSubscribeList = new ArrayList<ContentSubscribe>();
			// 读取数据
			while (cursor.moveToNext()) {
				ContentSubscribe contentSubscribe = new ContentSubscribe();
				contentSubscribe.setContentSubscribeId(cursor.getInt(cursor
						.getColumnIndex(CONTENT_SUBSCRIBE_ID)));
				contentSubscribe.setContentSubscribeName(cursor
						.getString(cursor
								.getColumnIndex(CONTENT_SUBSCRIBE_NAME)));
				contentSubscribe.setContentSubscribeUrl(cursor.getString(cursor
						.getColumnIndex(CONTENT_SUBSCRIBE_URL)));
				contentSubscribe.setContentSubscribeTime(cursor.getLong(cursor
						.getColumnIndex(CONTENT_SUBSCRIBE_TIME)));
				contentSubscribeList.add(contentSubscribe);
			}

		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (cursor != null) {

				cursor.close();

			}
			mGeneralDataBaseTemplate.close();

		}
		return contentSubscribeList;
	}

	/**
	 * 
	 * 根据ID删除预约节目 <br>
	 * 同步方法
	 * 
	 * @param contentSubscribeId
	 *            预约节目ID
	 * @return int 操作结果<br>
	 *         1 删除成功<br>
	 *         -1 删除异常<br>
	 */
	public int deleteContentSubscribeById(int contentSubscribeId) {
		try {

			// 删除本地数据库
			StringBuilder sql = new StringBuilder();
			sql.append("delete from ").append(TB_CONTENT_SUBSCRIBE);
			sql.append(" where ");
			sql.append(CONTENT_SUBSCRIBE_ID).append("=");
			sql.append(contentSubscribeId);

			mGeneralDataBaseTemplate.open();
			mGeneralDataBaseTemplate.delete(sql.toString());

			return 1;

		} catch (Exception e) {
			e.printStackTrace();
			L.e(TAG, "deleteContentSubscribeById", e.getMessage());
			return -1;
		} finally {
			mGeneralDataBaseTemplate.close();
		}

	}

	/**
	 * 
	 * 根据时间删除小于参数时间的预约节目 <br>
	 * 同步方法
	 * 
	 * @param contentSubscribeTime
	 *            预约节目时间
	 * @return int 操作结果<br>
	 *         1 删除成功<br>
	 *         -1 删除异常<br>
	 */
	public int deleteContentSubscribeByTime(long contentSubscribeTime) {
		try {

			// 删除本地数据库
			StringBuilder sql = new StringBuilder();
			sql.append("delete from ").append(TB_CONTENT_SUBSCRIBE);
			sql.append(" where ");
			sql.append(CONTENT_SUBSCRIBE_TIME).append("<");
			sql.append(contentSubscribeTime);

			mGeneralDataBaseTemplate.open();
			mGeneralDataBaseTemplate.delete(sql.toString());

			return 1;

		} catch (Exception e) {
			e.printStackTrace();
			L.e(TAG, "deleteContentSubscribeByTime", e.getMessage());
			return -1;
		} finally {
			mGeneralDataBaseTemplate.close();
		}

	}
}
